Skip to content

🐛(backend) Fix unreachable exception handler for URLValidator#2172

Merged
lunika merged 1 commit intosuitenumerique:mainfrom
maboukerfa:fix/urlvalidator-exception-handling
Apr 10, 2026
Merged

🐛(backend) Fix unreachable exception handler for URLValidator#2172
lunika merged 1 commit intosuitenumerique:mainfrom
maboukerfa:fix/urlvalidator-exception-handling

Conversation

@maboukerfa
Copy link
Copy Markdown
Contributor

Purpose

The exception block was never being executed because URLValidator raises django.core.exceptions.ValidationError, not drf.exceptions.ValidationError, so the except block was dead code.

Proposal

Replace the drf.exceptions.ValidationError with Django 'ValidationError'

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 3, 2026

Walkthrough

The change modifies error handling in the CORS proxy URL validation logic by switching from catching Django REST Framework's ValidationError to Django's native ValidationError exception type. A corresponding test update reflects the new error response format, changing the expected payload from a top-level list to an object with a detail field containing the error message as a string representation.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing the exception handler to catch the correct ValidationError type from Django instead of DRF.
Description check ✅ Passed The description is directly related to the changeset, explaining the bug (wrong exception type caught) and the fix (using Django's ValidationError).
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@lunika
Copy link
Copy Markdown
Member

lunika commented Apr 4, 2026

You are right, the exception is managed by the exception handler but not caught here.
The test should be failing with this modification; you have to update them.

@maboukerfa maboukerfa force-pushed the fix/urlvalidator-exception-handling branch from e1e9918 to e6c0f30 Compare April 4, 2026 13:31
@maboukerfa
Copy link
Copy Markdown
Contributor Author

You are right, the exception is managed by the exception handler but not caught here. The test should be failing with this modification; you have to update them.

Thank you!
I updated the related test.
Screenshot 2026-04-04 at 15 38 19

@lunika
Copy link
Copy Markdown
Member

lunika commented Apr 10, 2026

Thank you for your contribution.
I will rebase, fix the linting and merge it.

The exception block was never being executed because
URLValidator raises django.core.exceptions.ValidationError,
not drf.exceptions.ValidationError.

Signed-off-by: Mohamed El Amine BOUKERFA <boukerfa.ma@gmail.com>
@lunika lunika force-pushed the fix/urlvalidator-exception-handling branch from e6c0f30 to 45abefb Compare April 10, 2026 13:06
@lunika lunika enabled auto-merge (squash) April 10, 2026 13:09
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/backend/core/api/viewsets.py`:
- Around line 2138-2141: The exception handler currently returns a stringified
ValidationError via {"detail": str(e)}; change it to return structured
validation data instead: extract the validation payload from the exception
(prefer e.message_dict if present, then e.messages, then e.detail) and use that
value as the "detail" in the drf.response.Response so clients get
machine-readable error structures; update the except block that catches
ValidationError in viewsets.py (the variable e and the drf.response.Response
call) to build and return this structured payload instead of str(e).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 428d53ed-5c76-474b-adee-00820d53188c

📥 Commits

Reviewing files that changed from the base of the PR and between d0bf24f and 45abefb.

📒 Files selected for processing (2)
  • src/backend/core/api/viewsets.py
  • src/backend/core/tests/documents/test_api_documents_cors_proxy.py

Comment on lines +2138 to 2141
except ValidationError as e:
return drf.response.Response(
{"detail": str(e)},
status=drf.status.HTTP_400_BAD_REQUEST,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Return structured validation details instead of a stringified list

On Line 2140, {"detail": str(e)} produces a list-as-string payload ("['Enter a valid URL.']"). Prefer a structured detail to keep the API stable and machine-friendly.

💡 Proposed change
-        except ValidationError as e:
+        except ValidationError as e:
             return drf.response.Response(
-                {"detail": str(e)},
+                {"detail": e.messages},
                 status=drf.status.HTTP_400_BAD_REQUEST,
             )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
except ValidationError as e:
return drf.response.Response(
{"detail": str(e)},
status=drf.status.HTTP_400_BAD_REQUEST,
except ValidationError as e:
return drf.response.Response(
{"detail": e.messages},
status=drf.status.HTTP_400_BAD_REQUEST,
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/backend/core/api/viewsets.py` around lines 2138 - 2141, The exception
handler currently returns a stringified ValidationError via {"detail": str(e)};
change it to return structured validation data instead: extract the validation
payload from the exception (prefer e.message_dict if present, then e.messages,
then e.detail) and use that value as the "detail" in the drf.response.Response
so clients get machine-readable error structures; update the except block that
catches ValidationError in viewsets.py (the variable e and the
drf.response.Response call) to build and return this structured payload instead
of str(e).

@lunika lunika merged commit 1ebdda8 into suitenumerique:main Apr 10, 2026
22 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants